package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BusinessException;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;

    @Override
    @Transactional
    public void saveStmeal(SetmealDto setmealDto) {
        this.save(setmealDto);

        Long setmealId = setmealDto.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
        }
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
            qw.eq(Setmeal::getStatus, 1).eq(Setmeal::getId, id);
            Setmeal setmeal = this.getOne(qw);
            if (setmeal != null) {
                String setmealName = setmeal.getName();
                throw new BusinessException("套餐: " + setmealName + " 正在售卖,无法删除");
            }
        }

        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(qw2);
        }

        super.removeByIds(ids);
    }

    @Override
    public SetmealDto getSetmealDto(Long id) {
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getId, id);
        Setmeal setmeal = this.getOne(qw);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtil.copyProperties(setmeal, setmealDto);

        LambdaQueryWrapper<SetmealDish> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(qw2);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    @Override
    public void updateByIdWithSetmealDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);

        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(qw);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public List listDishes(Long id) {
        LambdaQueryWrapper<SetmealDish> qw=new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(qw);


        List<SetmealDishDto> setmealDishDtos = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtil.copyProperties(setmealDish,setmealDishDto);
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            String image = dish.getImage();
            setmealDishDto.setImage(image);
            setmealDishDtos.add(setmealDishDto);
        }


        return setmealDishDtos;
    }

}



